import 'package:flutter/material.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/common/langs/text_key.dart';
import 'package:oramemo/core/controllers/auth_controller.dart';
import 'package:oramemo/core/controllers/profile_controller.dart';
import 'package:oramemo/core/models/api_response.dart';
import 'package:oramemo/widget/gradient_app_head.dart';
import 'package:oramemo/widget/gradient_background.dart';

class NicknamePage extends StatelessWidget {
  final textController = TextEditingController();

  NicknamePage({super.key});

  @override
  Widget build(BuildContext context) {
    ProfileController controller = Get.find();
    return Scaffold(
      body: Column(
        children: [
          GradientAppHead(
            title: TextKey.nicknameTitle.tr,
            actions: TextButton(
              onPressed: () async {
                if (textController.text.isNotEmpty) {
                  ApiResponse? data = await controller.updateProfile(
                      nickname: textController.text);
                  if (data?.isSuccess == true) {
                    final logic = Get.find<AuthController>();
                    logic.getUserInfo();
                    Toast.show(TextKey.nicknameSuccess.tr);
                    Get.back();
                  }
                } else {
                  Toast.show(TextKey.nicknameEmptyError.tr);
                }
              },
              child: Text(
                TextKey.nicknameSave.tr,
                style: const TextStyle(
                  color: Colors.black,
                  fontSize: 16,
                ),
              ),
            ),
          ),
          Expanded(
            child: GradientBackground(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: TextField(
                        controller: textController,
                        decoration: InputDecoration(
                          hintText: TextKey.nicknameHint.tr,
                          contentPadding: EdgeInsets.symmetric(
                            horizontal: 16,
                            vertical: 12,
                          ),
                          border: InputBorder.none,
                        ),
                        style: TextStyle(
                          fontSize: 16,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
